package ms.unicorn;

import java.util.HashMap;
import java.util.Map;

import ms.unicorn.impl.EditingDomainServiceImpl;
import ms.unicorn.impl.NotificationServiceImpl;

public class UnicornService {
	
	private static Map<Class<?>, Object> services = new HashMap<Class<?>, Object>();
	
	static {
		registerService(NotificationService.class, new NotificationServiceImpl());
		registerService(EditingDomainService.class, new EditingDomainServiceImpl());
	}
	
	public static void registerService(Class<?> serviceType, Object instance) {
		services.put(serviceType, instance);
	}
	
	public static <T> T getService(Class<T> serviceType)  {
		@SuppressWarnings("unchecked")
		T instance = (T) services.get(serviceType);
		if (instance == null)
			try {
				registerService(serviceType, instance = serviceType.newInstance());
			} catch (InstantiationException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		return instance;
	}
	
	public static NotificationService getNotificationService() {
		return getService(NotificationService.class);
	}
	
	public static EditingDomainService getEditingDomainService() {
		return getService(EditingDomainService.class);
	}
}
